#!/usr/bin/python

#This file is for showing the data in a heightfield file
#it simply shows it all on standard out

import sys
import Numeric
import array
if(len(sys.argv) < 2):
 print "usage ", sys.argv[0], " <file name>"
else:
 #read the header data
 file = open(sys.argv[1], 'r')
 line = file.readline()
 tokens = line.split()
 ncols = int(tokens[0])
 nrows = int(tokens[1])
 zmin = float(tokens[2])
 zmax = float(tokens[3])
 print "Number of columns: ", ncols
 print "Number of rows: ", nrows
 print "zmin: ", zmin
 print "zmax: ", zmax
 #get the header data out of the file
 file.close()
 file = open(sys.argv[1], 'rb')
 contents = file.read()
 header_str = str(ncols)
 header_str = header_str + " " + str(nrows)
 header_str = header_str + " " + str(zmin)
 header_str = header_str + " " + str(zmax) + " "
 #get all the data left into a temporary file
 print "cuttin off ", len(header_str), " characters from the header"
 contents = contents[len(header_str):]
 print "first character in the new contents: '" + contents[0] + "'"
 tmpfile = open("tmp", 'wb')
 tmpfile.write(contents)
 tmpfile.close()
 tmpfile = open("tmp", 'rb')
 binvalues = array.array('f')
 binvalues.read(tmpfile, nrows * ncols)
 data = Numeric.array(binvalues, typecode=Numeric.Float)
 data = Numeric.reshape(data, (nrows, ncols))
 #now just print out the data
 for i in range(0, nrows-1):
  for j in range(0, ncols-1):
   print data[i][j], " "
